package com.example.meterialproject.activitys

import android.annotation.SuppressLint
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.SeekBar
import androidx.activity.viewModels
import androidx.core.view.forEach
import androidx.core.view.forEachIndexed
import com.example.meterialproject.base.BaseActivity
import com.example.meterialproject.databinding.ActivityNavigationBottomBinding
import com.example.meterialproject.exts.dp
import com.example.meterialproject.exts.showSnack
import com.example.meterialproject.view_models.NavigationBottomViewModel
import com.google.android.material.badge.BadgeDrawable
import com.google.android.material.chip.Chip
import com.google.android.material.navigation.NavigationBarView.*
import com.google.android.material.radiobutton.MaterialRadioButton

/**
 *
 * @ClassName: NavigationBottomActivity
 * @Author: android 超级兵
 * @CreateDate: 1/22/22$ 3:44 PM$
 * TODO NavigationBottom
 */
class NavigationBottomActivity : BaseActivity<ActivityNavigationBottomBinding>() {


    private val viewModel: NavigationBottomViewModel by viewModels()

    private val badgeGravityValues = intArrayOf(
        BadgeDrawable.TOP_END, // 右上
        BadgeDrawable.TOP_START, // 左上
        BadgeDrawable.BOTTOM_END, // 右下
        BadgeDrawable.BOTTOM_START // 左下
    )

    private val styleArrayList = intArrayOf(
        LABEL_VISIBILITY_AUTO, // item 少于等于 3 个时，标题处于显示状态；大于等于 4 个，选中才显示标题
        LABEL_VISIBILITY_SELECTED, // 选中才显示标题；
        LABEL_VISIBILITY_LABELED, // 一直显示
        LABEL_VISIBILITY_UNLABELED, // 只显示icon
    )

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        // 添加小红点
        binding.addRedBt.setOnClickListener {
            // 循环chipGroup
            binding.chipAddGroup.forEachIndexed { index: Int, view: View ->

                if (view is Chip) {
                    // 选中的chip
                    if (view.isChecked) {
                        Log.i("szj选中的chip", view.text.toString())

                        // 设置小红点
                        binding.bottomNavigationView.apply {
                            val itemId = menu.getItem(index).itemId

                            getOrCreateBadge(itemId).apply {
                                isVisible = true
                                number += 1
                            }
                        }
                    }
                }
            }
        }
        // 小红点位置
        binding.selectPositionRadioGroup.setOnCheckedChangeListener { group, checkedId ->

            // 选中的view
            val checkView = group.findViewById<MaterialRadioButton>(checkedId)
            // 选中的位置
            val indexOfChild = group.indexOfChild(checkView)

            if (indexOfChild > badgeGravityValues.size) {
                "太长了...".showSnack(binding.bottomNavigationView)
                return@setOnCheckedChangeListener
            }

            // 设置小红点
            binding.bottomNavigationView.apply {
                // 循环所有menu
                menu.forEach {
                    val itemId = it.itemId
                    getOrCreateBadge(itemId).apply {
                        // 设置位置
                        badgeGravity = badgeGravityValues[indexOfChild]
                    }
                }
            }
        }

        // 点击 navigation 事件监听
        binding.bottomNavigationView.setOnItemSelectedListener { item ->
//            item.title.showSnack(binding.bottomNavigationView)
            binding.bottomNavigationView.apply {
                getOrCreateBadge(item.itemId).apply {
                    isVisible = false
                    number = 0
                }
            }
            true
        }

        // 长按事件拦截
        binding.bottomNavigationView.menu.forEach {
            binding.bottomNavigationView.findViewById<View>(it.itemId)
                .setOnLongClickListener { true }
        }


        // 设置 icon 大小
        binding.seekBar.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
            @SuppressLint("SetTextI18n")
            override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
                binding.bottomNavigationView.itemIconSize =
                    progress.dp(this@NavigationBottomActivity).toInt()

                binding.seekTextView.text = "icon大小:${progress}dp"
            }

            override fun onStartTrackingTouch(seekBar: SeekBar?) {
            }

            override fun onStopTrackingTouch(seekBar: SeekBar?) {
            }
        })

        // 单选样式
        binding.styleRadioGroup.setOnCheckedChangeListener { group, checkedId ->
            val checkView = group.findViewById<MaterialRadioButton>(checkedId)
            val indexOfChild = group.indexOfChild(checkView)

            if (indexOfChild > styleArrayList.size) {
                "太长了...".showSnack(binding.bottomNavigationView)
                return@setOnCheckedChangeListener
            }
            binding.bottomNavigationView.labelVisibilityMode = styleArrayList[indexOfChild]
        }


        // 初始化lottie 与 bottomNavView 结合起来
        viewModel.initBottomNavigationView(binding.lottieBottomNavView)
    }
}
